import CrowdCounting.Model.Util;
import CrowdCounting.RunnableTask.PythonRunTask;
import CrowdCountingMVC.CCControler.CrowdCountingController;
import CrowdCountingMVC.CCControler.CrowdCountingControllerInerface;
import CrowdCountingMVC.CCModel.CrowdCountingModel;
import CrowdCountingMVC.CCModel.CrowdCountingModelInterface;
import CrowdCountingMVC.CCModel.Task.ImageTask.LoadImageAndNotifyTask;
import CrowdCountingMVC.CCModel.Task.ImageTask.LoadWarningImageAndNotifyTask;
import CrowdCountingMVC.CCView.CCShow.CrowdCountingShow;
import org.junit.Test;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

;

public class ETest {

    @Test
    public void testCrowdCountingShow() {
        CrowdCounting.Model.CrowdCounting.CrowdCountingShow crowdCounting = new CrowdCounting.Model.CrowdCounting.CrowdCountingShow("images\\crowd_images", "images\\warning_images", 1000, 1000);
        crowdCounting.showCrowdCounting();
    }

    @Test
    public void testJFrame() throws InterruptedException {
        JFrame frame = new JFrame();
        frame.getContentPane().add(new JLabel(new ImageIcon("F:\\crowd_density_early_warning_system\\src\\number_1.jpg")));
        frame.pack();
        frame.setVisible(true);
        Thread.sleep(1000);
    }


    @Test
    public void testPythonIn2() throws IOException, InterruptedException {
        Process process = Runtime.getRuntime().exec("D:\\Anconda3\\envs\\tensorflow\\python F:\\\\crowd_density_early_warning_system\\\\src\\\\out_image_for_java.py");
        BufferedImage image = ImageIO.read(process.getInputStream());

        JFrame frame = new JFrame();
        frame.getContentPane().add(new JLabel(new ImageIcon(image)));
        frame.pack();
        frame.setVisible(true);
        Thread.sleep(1000);

    }

    @Test
    public void testPythonTask() throws InterruptedException {
        PythonRunTask pythonRunTask = new PythonRunTask("D:\\Anconda3\\envs\\tensorflow\\python", "F:\\\\crowd_density_early_warning_system\\\\src\\\\out_image_for_java.py", true);
        pythonRunTask.addInputValueToCmd("0.05");
        Thread pythonThread = new Thread(pythonRunTask);
        pythonThread.start();
        Util.sleepTime(3000);
        pythonRunTask.getInfo(pythonRunTask.getProcess());
        pythonRunTask.checkErrorInfo();
    }


    @Test
    public void testDelete(){
        Util.deleteDirectory(new File("F:\\crowd_density_early_warning_system\\images"));
    }

    @Test
    public void testCheckImage(){
        Util.checkImage(new File("F:\\crowd_density_early_warning_system\\images\\image0"));
    }

    @Test
    public void testCreateFile() {
        System.out.println(Util.createFile("file.txt"));
    }

    @Test
    public void testShow() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setSize(200, 200);
        JLabel label = new JLabel();
        frame.add(label);
        frame.setVisible(true);
        for (int i = 0; i <= 10; i++) {
            label.setText(String.valueOf(i));
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }


        Util.sleepTime(10000);

    }

    @Test
    public void getFileText() {
        JFrame frame = new JFrame("Input Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container content = frame.getContentPane();
        content.setLayout(new FlowLayout());
        JTextField textField = new JTextField(20);
        content.add(textField);
        textField.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.println("User entered: " + textField.getText());
            }
        });
        frame.pack();
        frame.setVisible(true);
        Util.sleepTime(10000);
    }

    @Test
    public void testIsNumber() {
        System.out.println(Util.isNumeric("0.01"));
    }

    @Test
    public void ccModel() {
        CrowdCountingModel crowdCountingModel = new CrowdCountingModel();
        crowdCountingModel.start();
    }

    @Test
    public void ImageRead() throws IOException, InterruptedException {
        LoadImageAndNotifyTask loadImageAndNotifyTask = new LoadWarningImageAndNotifyTask(new CrowdCountingModel(), "F:\\crowd_density_early_warning_system\\images\\crowd_images");
        loadImageAndNotifyTask.loadImage();
        new Thread(loadImageAndNotifyTask).start();
    }

    @Test

    public void MVCShow() {
        CrowdCountingModel crowdCountingModel = new CrowdCountingModel();
        crowdCountingModel.setCrowdImageFile(new File("images/crowd_images/image1.png"));

        CrowdCountingShow crowdCountingShow = new CrowdCountingShow(crowdCountingModel, null, 1000, 1000);
        crowdCountingShow.showCrowdCounting();

        crowdCountingModel.registerCrowdShowObserver(crowdCountingShow);
        crowdCountingModel.registerWarningShowObserver(crowdCountingShow);

        crowdCountingModel.start();
        Util.sleepTime(10000000);
    }

    @Test
    public void Controller() {
        CrowdCountingModelInterface ccModel = new CrowdCountingModel();
        CrowdCountingControllerInerface crowdCountingController = new CrowdCountingController(ccModel, 1000, 1200);
    }
}
